package com.william.jdbcplus.test.dao.impl;

import com.william.jdbcplus.core.query.QueryLambdaWrap;
import com.william.jdbcplus.test.config.DBUtil;
import com.william.jdbcplus.test.dao.UserDao;
import com.william.jdbcplus.test.entity.*;
import lombok.AllArgsConstructor;

import java.util.List;

@AllArgsConstructor
public class UserDaoImpl implements UserDao {
    @Override
    public List<DbMenu> getMenusByUserId(String userId) {
        //1、查询用户的角色id列表
        QueryLambdaWrap lambdaWrap = new QueryLambdaWrap();
        lambdaWrap
                .select(DbRole::getId)
                .from(DbRole.class)
                .join(DbUserRole.class, DbUserRole::getRoleId, DbRole::getId)
                .join(DbUser.class, DbUser::getId, DbUserRole::getUserId)
                .eq(DbUser::getId, userId);

        List<String> roleIds = lambdaWrap.queryList(String.class, DBUtil.getMysql());

        //2、 查询角色列表对应的菜单
        lambdaWrap.clear();
        lambdaWrap
                .selectDistinct(true)
                .selectAll(DbMenu.class)
                .from(DbMenu.class)
                .join(DbRoleMenu.class, DbRoleMenu::getMenuId, DbMenu::getId)
                .join(DbRole.class, DbRole::getId, DbRoleMenu::getRoleId)
                .in(DbRole::getId, roleIds)
                .orderByAsc(DbMenu::getSortNo);

        List<DbMenu> menuList = lambdaWrap.queryList(DbMenu.class, DBUtil.getMysql());
        return menuList;
    }
}
